package cc.ls51.cms.interceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author liulf
 */
@Component
@SuppressWarnings("ALL")
public class AuthorizationInterceptor extends HandlerInterceptorAdapter {
    private Logger logger = LoggerFactory.getLogger(getClass());
    /**
     * 进入controller层之前拦截请求
     * @param o
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        logger.info(request.getRequestURI());
//        // 本地测试
//        if(myConfig.getCrossdomain().equals("*")){
//            response.setHeader("Access-Control-Allow-Origin",myConfig.getCrossdomain());
//            response.setHeader("Access-Control-Allow-Methods" , "GET,POST,PUT,DELETE,OPTIONS");
//            response.setHeader("Access-Control-Allow-Headers", "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
//        }
//        if(myConfig.isStartInterceptor()){
//            ResultObject ro = ResultObject.SuccessResult();
//            try {
//                String token = TokenUtils.getInstance().getToken();
//                if (!JwtUtil.checkJWT(token)) {
//                    ro.setCode(Integer.parseInt(ExceptionEnum.token_ERROR.getErrorCode()));
//                    ro.setMsg(ExceptionEnum.token_ERROR.getErrorMsg());
//                    renderUtil.renderJson(response, ro);
//                    return false;
//                }
//                boolean result = isHavePermission((HandlerMethod) handler,token);
//                if(!result){
//                    ro.setCode(0);
//                    ro.setMsg("非法访问，请联系管理员");
//                    renderUtil.renderJson(response, ro);
//                    return false;
//                }
//            }catch (BusinessException e){
//                ro.setCode(Integer.parseInt(ExceptionEnum.token_ERROR.getErrorCode()));
//                ro.setMsg(e.getErrorMsg());
//                renderUtil.renderJson(response,ro);
//                return false;
//            } catch (Exception e) {
//                ro.setCode(Integer.parseInt(ExceptionEnum.token_ERROR.getErrorCode()));
//                ro.setMsg(e.getMessage());
//                renderUtil.renderJson(response,ro);
//                return false;
//            }
//        }
        return true;
    }

//    private Boolean isHavePermission(HandlerMethod handler, String token) throws BusinessException {
//        // token角色权限判断
//        HandlerMethod handlerMethod = handler;
//        Method method = handlerMethod.getMethod();
//        RequiredPermission requiresPermissions = method.getAnnotation(RequiredPermission.class);
//        // 如果方法上的注解为空 则获取类的注解
//        if (requiresPermissions == null) {
//            requiresPermissions = handlerMethod.getMethod().getDeclaringClass().getAnnotation(RequiredPermission.class);
//        }
//        if (requiresPermissions == null) {
//            // 如果注解为null, 说明不需要拦截, 直接放过
//            return true;
//        }
//        String[] roles = requiresPermissions.value();
//        if(roles == null || roles.length==0){
//            return true;
//        }
//        String userRole = TokenUtils.getInstance().getUserRole(token);
//        if (!Arrays.asList(roles).contains(userRole)) {
//            return false;
//        }
//        return true;
//    }
}